cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

if(COMMAND cmake_policy)
  cmake_policy(SET CMP0002 NEW)
  cmake_policy(SET CMP0003 NEW)
  cmake_policy(SET CMP0004 NEW)
  cmake_policy(SET CMP0005 NEW)
  cmake_policy(SET CMP0008 NEW)
  cmake_policy(SET CMP0010 NEW)
  cmake_policy(SET CMP0012 NEW)
  cmake_policy(SET CMP0013 NEW)
  cmake_policy(SET CMP0014 NEW)
  cmake_policy(SET CMP0015 NEW)
  cmake_policy(SET CMP0016 NEW)
endif()

include(CheckIncludeFile)
include(CheckIncludeFileCXX)
include(CheckSymbolExists)
include(CheckLibraryExists)
include(CheckCXXSourceCompiles)

project(ScriptKit)

set(CMAKE_BUILD_TYPE Debug)

set(ScriptKit_VERSION_MAJOR 0)
set(ScriptKit_VERSION_MINOR 1)
set(ScriptKit_VERSION_PATCH 0)

set(ScriptKit_VERSION
	"${ScriptKit_VERSION_MAJOR}.${ScriptKit_VERSION_MINOR}.${ScriptKit_VERSION_PATCH}")

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules")

set(OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${OUTPUT_PATH})
set(EXECUTABLE_OUTPUT_PATH ${OUTPUT_PATH})

if(WIN32)
  if(CMAKE_SYSTEM_NAME MATCHES "Windows")
    set(OS_WIN32 TRUE)
  endif()
  if(MSVC)
    set(CMAKE_BUILD_ON_VISUAL_STUDIO 1)
  endif()
endif()

# Global compiler flags
if(CMAKE_COMPILER_IS_GNUCXX MATCHES "1")
  set(CMAKE_CXX_FLAGS "-std=gnu++0x -fno-rtti -Wall") #-Weffc++
  set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g -ggdb -D_DEBUG -O")
  set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O2")
  set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS} -g -ggdb -O2")
elseif(CMAKE_BUILD_ON_VISUAL_STUDIO MATCHES "1")
  set(CMAKE_CXX_FLAGS "/Y- /EHsc /GR- /Wall /WL /W4 /wd4100 /wd4127 /wd4180 /wd4251 /wd4480 /wd4482 /wd4800")
  set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} /MDd -D_DEBUG /RTC1 /GS /Zi /Od")
  set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} /MD -DNDEBUG /O2")
  set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS} /MD -DNDEBUG /Zi /O2")
  set(CMAKE_CXX_STANDARD_LIBRARIES "User32.lib Kernel32.lib") # FIXME this doesn't belong here
endif()

check_include_file_cxx(thread HAVE_STL_THREAD)
check_include_file_cxx(cstdint HAVE_CSTDINT)
check_include_file(unistd.h HAVE_UNISTD_H)
check_include_file(dlfcn.h HAVE_DLFCN_H)
	
find_package(Spidermonkey REQUIRED)

if(NOT HAVE_STL_THREAD)
  set(Boost_USE_STATIC_LIBS ON)
  find_package(Boost COMPONENTS thread date_time REQUIRED)
endif()

if(UNIX)
  if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    set(OS_LINUX TRUE)
    check_library_exists(dl dlopen "" HAVE_LIBDL)
    if(NOT HAVE_LIBDL)
      message(FATAL_ERROR "Cannot find libdl")
    endif()
  endif()
endif()

enable_testing()

add_subdirectory(ScriptKit)
add_subdirectory(ConsoleDemo)

